package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.WaContactUnit;
import com.ruoyi.system.service.WaContactUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 联络单位的controller层
 */
@RestController
@RequestMapping("/wa/unit")
public class WaContactUnitController extends BaseController {
    @Autowired
    private WaContactUnitService contactUnitService;
    /**
     *
     * @param waContactUnit
     * @return  新增联络单位
     */
    @PostMapping
    public AjaxResult add( @RequestBody WaContactUnit waContactUnit) {
        waContactUnit.setCreateBy(SecurityUtils.getUsername());
        return toAjax(contactUnitService.add(waContactUnit));
    }

    /**
     * 获得用户
     * @param unitName
     * @return
     */
    @Anonymous
    @GetMapping
    public AjaxResult listUnits(@RequestParam(required = false) String unitName)
    {
        List<WaContactUnit> units = contactUnitService.listUnitsWithUsers(unitName);
        return AjaxResult.success(units);
    }
    /**
     * 删除联络单位，是逻辑删除，在页面上不显示在数据库中依然存在
     */
    @DeleteMapping("/{unitId}")
    public AjaxResult removeUnit(@PathVariable long unitId)
    {
        return toAjax(contactUnitService.deleteUnitById(unitId));
    }

    /**
     * 修改联络单位的信息
     * @param contactUnit
     * @return
     */
  @PutMapping
  public AjaxResult edit( @RequestBody WaContactUnit contactUnit)
  {
      contactUnit.setUpdateBy(SecurityUtils.getUsername());
      return toAjax(contactUnitService.updateUnit(contactUnit));
  }

}
